<?php

namespace app\api\service;

use think\Controller;
use think\Db;
use think\Exception;

class Shop extends Controller {

    /**
     * 获取门店首頁数据
     * @param $param
     * @return array
     */
    public static function get_shop_index_info($param) {
        try {
            $data = model('shop')->get(['member_id' => $param['member_id']]);

            // logo
            $data->logo = get_img_url($data->logo);

            // 门店图片
            $data->store_img_1 = get_img_url($data->store_img_1);
            $data->store_img_2 = get_img_url($data->store_img_2);
            $data->store_img_3 = get_img_url($data->store_img_3);

            // 营业执照图片
            $data->business_license_image = get_img_url($data->business_license_image);

            $data['nickname'] = $data->shopMember->memberRoleInfo->nickname ? $data->shopMember->memberRoleInfo->nickname : '';  // 昵称
            $data['headimgurl'] = $data->shopMember->memberRoleInfo->headimgurl ? get_img_url($data->shopMember->memberRoleInfo->headimgurl) : '';  // 头像

            return api_return(200, $data);
        } catch (\Exception $e) {
            return api_return(0, [], $e->getMessage());
        }
    }

    /**
     * 获取门店数据
     * @param $param
     * @return array
     */
    public static function get_shop_info($param) {
        try {
            $data = model('shop')->get(['member_id' => $param['member_id']]);
            $data['host_logo'] = $data->logo != '' ? get_img_url($data->logo) : '';
            $data['host_business_license_image'] = $data->business_license_image != '' ? get_img_url($data->business_license_image) : '';

            return api_return(200, $data);
        } catch (\Exception $e) {
            return api_return(0, [], $e->getMessage());
        }
    }

    /**
     * 保存门店申请信息
     * @param $param
     * @return array
     */
    public static function save_shop($param) {
        try {
            $id = $param['id'];
            $member_id = $param['member_id'];
            $data = [
                'name' => $param['name'],
                'mobile' => $param['mobile'],
                'address' => $param['address'],
                'shopkeeper' => $param['shopkeeper'],
                'logo' => $param['logo'],
                'store_img_1' => $param['store_img_1'],
                'store_img_2' => $param['store_img_2'],
                'store_img_3' => $param['store_img_3'],
                'business_license_image' => $param['business_license_image'],
                'business_license_registration_number' => $param['business_license_registration_number']
            ];

            if ($id > 0) {
                model('shop')->save($data, ['id' => $id, 'member_id' => $member_id]);
            } else {
                $data['member_id'] = $member_id;
                model('shop')->save($data);
            }
            return api_return(200, []);
        } catch (\Exception $e) {
            return api_return(0, [], $e->getMessage());
        }
    }

    /**
     * 新订单通知
     * @param $param
     * @return array
     */
    public static function inform($param) {
        try {
            file_put_contents('inform.txt', '开始通知' . " \n ");
            // 获取订单信息
            $order = model('order')->get(['id' => $param['order_id']]);
            file_put_contents('inform.txt', json_encode($order) . "---订单 \n ", FILE_APPEND);
            // 根据订单收获地址  获取市、区 对应的ID   （地址名称有重复   通过市、区一起来选择对应的量体师）
            $city = model('region')->get(['region_name' => $order['city']]);
            $area = model('region')->get(['region_name' => $order['area'], 'parent_id' => $city['region_id']]);
            // 查询量体师
            $pattern_master_s = model('pattern_master')->all(['area' => $area->region_id, 'status' => 2]);
            file_put_contents('inform.txt', json_encode($pattern_master_s) . "---量体师 \n ", FILE_APPEND);
            foreach ($pattern_master_s as $v) {
                file_put_contents('inform.txt',  "---量体师-------- \n ", FILE_APPEND);
                $mobile = $v->patternMasterMember->username;
                $openid = $v->patternMasterMember->openid;
                file_put_contents('inform.txt',  $mobile  . '--' . $openid .  "---量体师-------- \n ", FILE_APPEND);
                $address = $order->province . $order->city . $order->area . $order->address;
                $param = [
                    'mobile' => $mobile,
                    'type' => 2,
                    'order_sn' => $order->order_sn,
                    'address' => $address,
                    'linkman' => $order->consignee,
                    'address_mobile' => $order->mobile
                ];
                file_put_contents('inform.txt', json_encode($param) . "---量体师 \n ", FILE_APPEND);
                \app\api\service\NoteCode::send_code($param);

                /* +  -----    订单生成成功，发送微信模板消息通知  ----- + */
                if ($openid) {
                    $param = [
                        'touser' => $openid,
                        'template_id' => '-wjkS0vN_NOaiYY6biMRlySepDiPxzUu3TfcFb5FQnk',
                        'url' => request()->domain() . url('pattern_master/info', ['id' => $order->id]),
                        'data' => [
                            'first' => [ 'value' => '发现新订单', 'color' => '#173177'],
                            'keyword1' => [ 'value' => $order->order_sn, 'color' => '#173177'],
                            'keyword2' => [ 'value' => date('Y-m-d H:i:s', $order->create_time), 'color' => '#173177'],
                            'remark' => [ 'value' => "请尽快处理 \n 订制人：" . $order->consignee . " \n 联系电话：" . $order->mobile . " \n 收货地址：" . $address, 'color' => '#173177'],
                        ]
                    ];
                    file_put_contents('inform.txt', json_encode($param) . "---量体师 \n ", FILE_APPEND);
                    send_create_order_wechat_tmplmsg(json_encode($param));
                }
            }
        } catch (\Exception $e) {
            return api_return(0, [], $e->getMessage());
        }
    }


    /**
     * 获取店铺的量体师
     * @param $param
     * @return array
     */
    public static function get_my_pattern_master($param) {
        try {
            $pattern_master = model('pattern_master')->all(['relevance_shop_id' => $param['shop_id']]);
            foreach ($pattern_master as $v) {
                $initial = getfirstchar(substr($v->name, 0, 3));
                $data[$initial][] = [
                    'name' => $v->name,
                    'headimgurl' => $v->patternMasterMember->memberRoleInfo->headimgurl ? get_img_url($v->patternMasterMember->memberRoleInfo->headimgurl) : ''
                ];
            }
            ksort($data);
            return api_return(200, $data);
        } catch (\Exception $e) {
            return api_return(0, [], $e->getMessage());
        }
    }
}
